/**
 * 功能模块维护
 * @author yuhuan
 * @date 2018/12/24 15:35
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        table,
        vueData = {
            defaultProps: {
                children: 'children',
                label: 'name'
            },
            treeData: []
        },
        table_where = {
            cascade: '1',
            sort: null
        },
        cols = [[
            {field: 'ROW_ID', width: 60, title: '序号'}
            , {field: 'NAME', width: 120, title: '模块名称', sort: true, search: 'default', searchOpts: {type: 'input'}}
            // , {field: 'NAME_TW', width: 120, title: '繁体名称'}
            // , {field: 'NAME_EN', width: 120, title: '英文名称'}
            , {field: 'URL', width: 120, title: '请求地址'}
            , {field: 'PERMISSION', width: 120, title: '权限标识'}
            , {field: 'SORT_NO', width: 70, title: '排序号'}
            , {field: 'ICON_NAME', width: 100, title: '图标'},
            // {field: 'VECTOR', width: 100, title: '矢量图标'},
            {
                field: 'IS_LEAF', width: 80, title: '叶子节点',
                filterDatas: app.datas_('sys_module_status')
            },
            {
                field: 'IS_ENABLE', width: 80, title: '是否启用',
                filterDatas: app.datas_('sys_module_status')
            },
            {
                width: 60,
                title: '',
                toolbar: '#script-modulesetting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        moduleNodeData,
        selectNode,
        moduleSettingAction,
        module_edit_openOpts = {
            maxmin: true,
            area: ['720px', '410px'],
            title: '功能模块信息编辑'
        }
    ;

    var Module = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'module',
                _searchs: [{
                    id: 'module', tableId: 'module_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'module_edit', tpl: 'system/module_edit'
                }]
            }, win);
        },
        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'module_table'),
                    url: g_ctxPath + 'sys/module/getModuleslist',
                    exportsName: '功能模块数据',
                    title: '功能模块信息表',
                    id: 'module_table',
                    autoSort: false, //禁用前端自动排序
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter'],
                    height: 'full',
                    cols: cols,
                    page: true,
                    where: table_where
                });

                layui.laytpl(app.getByid(doc, 'script-modulesetting-action').innerHTML).render({}, function (string) {
                    moduleSettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(module_table)', function (obj) {

                    layer.tips(moduleSettingAction, obj.td, {
                        tips: [3, '']
                        , time: 5000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            tipsIndex = index;
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'module_edit',
                                            tagData: {
                                                obj: obj,
                                                detail: false
                                            },
                                            index: 0,
                                            openOpts: module_edit_openOpts
                                        });
                                        break;
                                    case '1':
                                        if (obj.data.NAME == 'Digiwin智能移动应用平台') {
                                            app.showError('顶级父节点，不允许删除！');
                                            return;
                                        }
                                        self.delModule(obj);
                                        break;
                                }
                                layer.close(index);
                                tipsIndex = null;
                            });
                        }
                    });
                    return false;
                });

                //监听行双击事件
                table.on('rowDouble(module_table)', function (obj) {
                    self._openTplFull({
                        tag: 'module_edit',
                        tagData: {
                            obj: obj,
                            detail: true
                        },
                        index: 0,
                        openOpts: module_edit_openOpts
                    });
                });

                //监听事件
                table.on('toolbar(module_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'module_edit',
                                tagData: {
                                    is_add: true,
                                    cascade_id: selectNode.code,
                                    module_id: selectNode.id,
                                    module_name: selectNode.name,
                                    moduleNodeData: moduleNodeData
                                },
                                index: 0,
                                openOpts: module_edit_openOpts
                            });
                            break;
                        case 'delete':
                            layer.msg('删除');
                            self.delModule(obj);
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },
        reloadTableCurr: function () {
            self._reloadTable({id: 'module_table', where: table_where});
        },
        delModule: function (obj) {
            app.confirm({
                msg: '是否删除选择的信息？',
                ok: function () {
                    app.ajax({
                        xurl: 'sys/module/delete',
                        xdata: obj.data,
                        loading: true,
                        okmsg: '模块信息删除成功',
                        cb: function (result) {
                            if (result.ok) {
                                self.reloadTableCurr();
                                self.deleteNode(obj.data.ID);
                            }
                        }
                    });
                }
            });

        },
        initTree: function () {

            app.ajax({
                xurl: 'sys/module/getModulesTree',
                cb: function (result) {
                    if (result.ok) {
                        moduleNodeData = result.data.data;
                        vueData.treeData.pushArray(moduleNodeData);
                        selectNode = moduleNodeData[0];

                    }
                }
            });

        },
        updateTree:function() {
            app.ajax({
                xurl: 'sys/module/getModulesTree',
                async:false,
                cb: function (result) {
                    if (result.ok) {
                        moduleNodeData = result.data.data;
                        vueData.treeData = moduleNodeData;
                        selectNode = moduleNodeData[0];
                        table_where.module_cascade_id = selectNode.code;
                        table_where.module_id = selectNode.id;
                    }
                }
            });
        },
        addNode: function (node) {
            self.vues['page'].$refs.tree.append(node, node.parentid);
        },
        editNode: function (id, name) {
            self.vues['page'].$refs.tree.getNode(id).data.name = name;
        },
        deleteNode: function (id) {
            self.vues['page'].$refs.tree.remove(id);
        },
        _ready: function () {
            vueData.serchData = table_where;
            this._super({
                vue: [{
                    el: '#page',
                    id: 'page',
                    data: vueData,
                    searchId: 'module',
                    methods: {
                        handleNodeClick: function (node) {
                            selectNode = node;
                            table_where.module_cascade_id = node.code;
                            table_where.module_id = node.id;
                            self._reloadTable({id: 'module_table', curr: 1, where: table_where});
                        },
                        queryData: function () {
                            self._reloadTable({id: 'module_table', curr: 1, where: table_where});
                        }
                    }
                }]
            });
            self.initTable();
            self.initTree();

        },
        _lazy: function () {
            this._super();
            var treeH = document.querySelector('.el-tree');
            var tableH = document.querySelector('.layui-form').clientHeight;
            treeH.style.height = (tableH + 2) + 'px';

            $(window).resize(function () {
                var tableH = document.querySelector('.layui-form').clientHeight;
                treeH.style.height = (tableH + 2) + 'px';
            })
        }
    });

    return new Module();

});